<template>
  <div class="detail">
    <detail-nav-bar
      class="detail-nav"
      @navIndex="itemClick"
      ref="navBar"
    ></detail-nav-bar>
    <scroll
      class="dcontent"
      ref="scroll"
      :probe-type="3"
      @scroll="scrollToPosition"
    >
      <detail-swiper :topImages="topImages" ref="deSwiper"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @goodsImageLoad="goodsImageLoad"
      ></detail-goods-info>
      <detail-param-inf :paramInfo="paramInfo" ref="deParam"></detail-param-inf>
      <detail-comment-info
        :commentInfo="commentInfo"
        ref="deComment"
      ></detail-comment-info>
      <goods-list :goods="recommendList" ref="deGoods"></goods-list>
    </scroll>
    <back-top @click.native="backTopClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addToCar="addToCar"></detail-bottom-bar>
  </div>
</template>

<script>
// 导入组件
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInf from "./childComps/DetailParamInf.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import GoodsList from "components/content/goods/GoodsList";

import Scroll from "../../components/common/scroll/Scroll.vue";
// import { debounce } from "common/utils";

import DetailBottomBar from "./childComps/DetailBottomBar";
import BackTop from "components/content/backTop/BackTop";

import { imgListenerMixin, backTopMixin } from "common/mixin";

// 网络请求数据
import {
  getDetailData,
  getRecommend,
  Goods,
  Shop,
  GoodsParam,
} from "network/detail";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInf,
    DetailCommentInfo,
    GoodsList,
    Scroll,
    DetailBottomBar,
    BackTop,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendList: [],
      offSetTopY: [],

      navIndex: 0,
    };
  },
  mixins: [imgListenerMixin, backTopMixin],

  created() {
    this.iid = this.$route.params.iid;
    this.getDetailData();
    this.getRecommend();

    // 获取推荐商品的数据
  },

  methods: {
    getDetailData() {
      getDetailData(this.iid).then((res) => {
        console.log(res);
        const data = res.result;

        // 顶部图片
        this.topImages = data.itemInfo.topImages;
        // 商品信息
        this.goods = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.service
        );
        // 商店信息
        this.shop = new Shop(data.shopInfo);

        // 4.保存商品的详情数据
        this.detailInfo = data.detailInfo;

        // 5.获取参数的信息
        this.paramInfo = new GoodsParam(
          data.itemParams.info,
          data.itemParams.rule
        );

        // 获取评论数据
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0] || {};
        }

        // this.$nextTick(() => {
        //   this.offSetTopY = [];
        //   this.offSetTopY.push(this.$refs.deSwiper.$el.offsetTop);
        //   this.offSetTopY.push(this.$refs.deParam.$el.offsetTop);
        //   this.offSetTopY.push(this.$refs.deComment.$el.offsetTop);
        //   this.offSetTopY.push(this.$refs.deGoods.$el.offsetTop);
        //   console.log(this.offSetTopY);
        // });
      });
    },

    getRecommend() {
      getRecommend().then((res) => {
        // console.log(res);
        this.recommendList = res.data.list;
      });
    },

    goodsImageLoad() {
      this.$refs.scroll.refresh();
      this.offSetTopY = [];
      this.offSetTopY.push(this.$refs.deSwiper.$el.offsetTop);
      this.offSetTopY.push(this.$refs.deParam.$el.offsetTop);
      this.offSetTopY.push(this.$refs.deComment.$el.offsetTop);
      this.offSetTopY.push(this.$refs.deGoods.$el.offsetTop);
      // console.log(this.offSetTopY);
    },

    // 点击跳到相应的区域
    itemClick(index) {
      this.navIndex = index;
      // console.log("danqian " + index);
      this.$refs.scroll.scrollTo(0, -this.offSetTopY[this.navIndex], 500);
      // console.log("wotiaole ");
    },

    addToCar() {
      console.log("wodianji le ");
      const Obj = {
        image: this.topImages[0],
        title: this.goods.title,
        desc: this.goods.desc,
        price: this.goods.realPrice,
        id: this.iid,
      };

      this.$store.dispatch("setCarList", Obj).then((res) => {
        this.$mytoast.toastShow(res, 3000);
      });
    },

    // 滑到相应区域，导航切换对应

    scrollToPosition(position) {
      const positionY = position ? -position.y : 0;
      this.isShowBackTop = -position.y > 1000;
      const length = this.offSetTopY.length;
      // this.offSetTopY[this.offSetTopY.length] = math;
      for (let i = 0; i < length; i++) {
        if (
          (i < length - 1 &&
            positionY > this.offSetTopY[i] &&
            positionY < this.offSetTopY[i + 1]) ||
          (i == length - 1 && positionY > this.offSetTopY[i])
        ) {
          this.navIndex = i;
        }
      }
      this.$refs.navBar.currentIndex = this.navIndex;
    },
  },
  destroyed() {
    console.log("销毁了");
    this.$bus.$off("imageItemload", this.imgListener);
  },
};
</script>

<style scoped>
.detail {
  height: 100vh;
  position: relative;
  z-index: 2;
}
.detail-nav {
  height: 44px;
}

.dcontent {
  position: absolute;
  top: 44px;
  bottom: 50px;
  left: 0;
  right: 0;
  width: 100%;
  /* height: clac(100vh - 44px); */
  overflow: hidden;
}
</style>